package se.boliang.Q03;

public class MaxSumSubArray {

	public int getMaxValue(int[] intArray) {

		int sumValue = 0;
		int maxValue = 0;

		for (int i : intArray) {

			/**
			 * If the current element's value is greater than the sum value of
			 * entire former elements. We should ignore the former value and
			 * start over from the current element.
			 */
			if (i >= sumValue + i) {
				maxValue = i;
				sumValue = i;

				/**
				 * Only when the 'sumValue' is greater than 'maxValue', then
				 * assign the greater value to 'maxValue'. It means the
				 * sub-array's size is increased.
				 */
			} else {

				if (i + sumValue < maxValue) {
					sumValue = i + sumValue;
				} else {
					sumValue = i + sumValue;
					maxValue = sumValue;
				}
			}
		}

		return maxValue;
	}
}
